package com.it.schoolbookshop_back.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * 整数到布尔值的序列化器
 * 用于将整数(1/0)转换为布尔值(true/false)
 * 
 * 使用方法：
 * 在DTO类的字段上添加 @JsonSerialize(using = IntegerToBooleanSerializer.class) 注解
 * 配合 @JsonDeserialize(using = BooleanToIntegerDeserializer.class) 实现双向转换
 */
public class IntegerToBooleanSerializer extends JsonSerializer<Integer> {

    @Override
    public void serialize(Integer value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        if (value == null) {
            gen.writeBoolean(false);
            return;
        }
        
        gen.writeBoolean(value == 1);
    }
} 